﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;

namespace Server.Network
{
    public class Serilizer
    {
        public Serilizer()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        public string EntityToJson(object entity)
        {
            DataContractSerializer serializer = new DataContractSerializer(entity.GetType());
            using (MemoryStream stream = new MemoryStream())
            {
                serializer.WriteObject(stream, entity);
                return Encoding.UTF8.GetString(stream.ToArray());
            }

        }

        public object JsonToEntity(string jsonstring, Type type)
        {
            DataContractSerializer serializer = new DataContractSerializer(type);
            using (MemoryStream mStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonstring)))
            {
                return serializer.ReadObject(mStream);
            }
        }
    }
}
